package org.someone.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.script.ScriptException;

import org.someone.formular.Calculator;

public class ListTools {
	/**
	 * 提取符合条件要求的数据并重新构造一个List
	 * @param conditionEval		提取数据的条件
	 * @param sources			源list
	 * @return					新的list
	 * @throws Exception 
	 */
	public static List<Map<String,String>> extract(String conditionEval, List<Map<String,String>> sources) throws Exception{
		List<Map<String,String>> result = new ArrayList<Map<String,String>>();
//		List<String> keys = getConditionKeys(conditionEval);
		for(int i=0; i < sources.size(); i++){
			Map<String,String> map = sources.get(i);
			if(Calculator.judgeCondition(conditionEval, map)){
				result.add(map);
			}
		}
		return result;
	}
	/**
	 * @param sources	old list
	 * @return
	 */
	public static List<Map<String,String>> cloneListMap(List<Map<String,String>> sources){
		if(sources == null) return null;
		List<Map<String,String>> clone = new ArrayList<Map<String,String>>();
		for(int i=0; i< sources.size(); i++){
			if(sources.get(i) != null){
				Map<String,String> t = new HashMap<String,String>();
				t.putAll(sources.get(i));
				clone.add(t);
			}else{
				clone.add(null);
			}
		}
		return clone;
	}
}
